package com.szy.system.common.util;

import com.szy.system.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhenyun.su
 * @comment :
 * 上传文件，保存文件
 * 下载文件，按文件名
 * 2021-03-01 ---
 *    InputStream inputStreamByresourceFile(String resourceFile)  获取jar类路径资源文件
 *    byte[] byteArrayByresourceFile(String resourceFile)
 * @since : 2020/1/22
 */

@Deprecated
public final class SzyFileUtils {
    private static final Logger logger = LoggerFactory.getLogger(SzyFileUtils.class);

    public static InputStream inputStreamByresourceFile(String resourceFile) throws Exception {
        ClassPathResource cpr = new ClassPathResource(resourceFile);
        return cpr.getInputStream();
    }

    public static byte[] byteArrayByresourceFile(String resourceFile) throws Exception {
        ClassPathResource cpr = new ClassPathResource(resourceFile);
        return FileCopyUtils.copyToByteArray(cpr.getInputStream());
    }

    /**
     * @comment : 保存文件
     * 文件已存在，则先删除，再保存
     * 目录不存在，自动创建
     */
    public static void saveFile(MultipartFile multipartFile, String fileName) throws Exception {
        File file = FileUtils.checkFile(fileName);
        multipartFile.transferTo(file);
    }

    public static void saveFile(InputStream in, String fileName) throws Exception {
        FileUtils.checkFile(fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        FileCopyUtils.copy(in, fileOutputStream);
    }

    public static void saveFile(byte[] value, String fileName) throws Exception {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(value);
        saveFile(byteArrayInputStream, fileName);
    }

    /**
     * @return :
     * [
     * {
     * "msg": "E:\\6_Java\\4_javasample\\springboot-simple\\upload\\1597884768816template.xlsx",
     * "exType": "xlsx",
     * "fileName": "template.xlsx",
     * "status": "success"
     * },
     * {
     * "msg": "E:\\6_Java\\4_javasample\\springboot-simple\\upload\\1597884768837template2.xlsx",
     * "exType": "xlsx",
     * "fileName": "template2.xlsx",
     * "status": "success"
     * }
     * ]
     * @comment : 保存多个文件， 空文件不保存
     */
    public static List<Map<String, String>> saveMultipartFile(List<MultipartFile> files, String subPath) {
        logger.debug("uploadFile, start save MultipartFile count {}", files.size());
        List<Map<String, String>> fileMsgs = new ArrayList<>();
        for (MultipartFile file : files) {
            Map<String, String> fileMsg = saveFile(file, subPath, false);
            fileMsgs.add(fileMsg);
        }
        return fileMsgs;
    }

    /**
     * @return :
     * @comment : 保存单个文件
     * @params : ifEmpty true 表示空文件也保存
     */
    public static Map<String, String> saveFile(MultipartFile file, String subPath, boolean ifEmpty) {
        logger.debug("uploadFile, start save MultipartFile");
        Map<String, String> fileMsg = new HashMap<>();
        try {
            String originalFilename = file.getOriginalFilename();
            String extType = FileUtils.getExtType(originalFilename);
            fileMsg.put("fileName", originalFilename);
            fileMsg.put("exType", extType);

            if (!ifEmpty && file.isEmpty()) {
                fileMsg.put("status", "failure");
                fileMsg.put("msg", "file content is empty");
                return fileMsg;
            }
            String newFileName = FileUtils.getAppTempFile(subPath, originalFilename);
            printFileInfo(file, newFileName);
            SzyFileUtils.saveFile(file, newFileName);
            fileMsg.put("status", "success");
            fileMsg.put("msg", newFileName);
        } catch (Exception e) {
            fileMsg.put("status", "failure");
            fileMsg.put("msg", e.getMessage());
            logger.error("uploadFile, exception: {}", e);
        }
        return fileMsg;
    }

    public static void printFileInfo(MultipartFile file, String fileName) {
        logger.debug("-------------------------------------------------");
        logger.debug("getSize: {}", file.getSize());
        logger.debug("getContentType: {}", file.getContentType());
        logger.debug("getName: {}", file.getName());
        logger.debug("getOriginalFilename: {}", file.getOriginalFilename());
        logger.debug("File location: {}", fileName);
        logger.debug("-------------------------------------------------");
    }
}
